package br.ufpe.cin.jfsr.tg.editor.builddtd;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.epsilon.egl.EglFileGeneratingTemplateFactory;
import org.eclipse.epsilon.egl.EglTemplateFactoryModuleAdapter;
import org.eclipse.epsilon.emc.emf.InMemoryEmfModel;
import org.eclipse.gmf.runtime.emf.core.resources.GMFResource;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;

import editor.EditorPackage;
import editor.diagram.part.EditorDiagramEditor;

public class BuildDtdAction implements IWorkbenchWindowActionDelegate {

	/**
	 * The constructor.
	 */
	public BuildDtdAction() {
	}

	@Override
	public void run(IAction action) {
		// Get the active editor
		IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();

		if(editor instanceof EditorDiagramEditor){
			EditorDiagramEditor xmlEditor = (EditorDiagramEditor) editor;

			// Get the EMF model under the editor
			Resource resource = getFirstSemanticModelResource(xmlEditor
					.getEditingDomain().getResourceSet());

			if (resource == null) return;

			InMemoryEmfModel m = new InMemoryEmfModel("M", resource,EditorPackage.eINSTANCE);

			// Construct the EGL module
			EglTemplateFactoryModuleAdapter module = 
					new EglTemplateFactoryModuleAdapter(new EglFileGeneratingTemplateFactory());

			try {
				URI uri = Activator.getDefault().getBundle().getResource("templates/ModelToDtd.egl").toURI();
				module.parse(uri);
				module.getContext().getModelRepository().addModel(m);
				
				IFile modelFile = ResourcesPlugin.getWorkspace().getRoot().
					getFile(new Path(resource.getURI().path().substring("/resource/".length())));
				
				FileOutputStream fos = new FileOutputStream(
						new File(modelFile.getLocation().toOSString() + ".dtd"));
				
				// Execute the EGL module and write the result to
				// a file next to the model
				fos.write((module.execute() + "").getBytes());
				fos.flush(); fos.close();
				
				modelFile.getParent().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
			}
			catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public Resource getFirstSemanticModelResource(ResourceSet resourceSet) {
		for (Resource resource : resourceSet.getResources()) {
			if (!(resource instanceof GMFResource)) {
				return resource;
			}
		}
		return null;
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IWorkbenchWindow window) {
		// TODO Auto-generated method stub

	}

}
